class Operating {

    bindEvent () {

		$('body').on('click', '.status', this.status.bind(this));
		$('body').on('click', '.edit', this.edit.bind(this));
		$('body').on('click', '.delete', this.delete.bind(this));
		$('body').on('click', '.edit-add', this.editAdd.bind(this));
	}

	status (ele) {
		let $ele = $(ele.target);
		let self = this;
        let status = !!$ele.data('status') ? 0 : 1;
		let reqData = {
			paged: true,
			pageNo: 1,
			pageSize: 10,
			status: status
		};
		reqData[$ele.data('key')] = $ele.data('id');

		$.ajax({
			url: $ele.data('url'),
			method: 'post',
			data: JSON.stringify(reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$ele.parent().prevAll().each(function () {

						if ($(this).data('name') === 'status') {
							$(this).text(status ? '已上线' : '未上线');
						}
					});
					$ele.data('status', status);
					$ele.text(status ? '撤下' : '发布');
				}
				else {
					alert('修改失败');
				}
			}
		});
	}

	edit (ele) {
		let $ele = $(ele.target);
		let self = this;

		$ele.parent().prevAll().each(function () {
			let text = $(this).text();
			let name = $(this).data('name');
			let val = $(this).data('value');

			$(this).html([
				'<div class="ui input">',
					'<input type="text" name="'+name+'" value="'+val+'" class="hidden">',
				'</div>'
			].join(''));
		});

		$ele.removeClass('deploy-edit').addClass('deploy-edit-add');
		$ele.text('修改');
	}

    editAdd (ele) {
		let $ele = $(ele.target);
		let self = this;
		let reqData = {
			paged: true,
        	pageNo: 1,
        	pageSize: 10
		};
		reqData[$ele.data('key')] = $ele.data('id');

		$ele.parent().prevAll().each(function() {

        	var elemArr = $('input,textarea', this);
        	elemArr = Array.prototype.slice.call(elemArr);

        	if (elemArr.length === 1) {

        		let elem = elemArr[0];
				reqData[elem.name] = elem.value;
        	}
        });

		$.ajax({
			url: $ele.data('url'),
			method: 'post',
			data: JSON.stringify(reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$ele.parent().prevAll().each(function () {

						let input = $(this).find('input')[0];
						let text = '';

						if (input) {
							if (input.name === 'status') {
								text = parseInt(input.value, 10) ? '已上线' : '未上线';
							}
							else {
								text = input.value;
							}
							$(this).text(text);
						}
					});
					$ele.removeClass('account-edit-add').addClass('account-edit');
					$ele.text('编辑');
				}
				else {
					alert('修改失败');
				}
			}
        });
	}

	delete (ele) {
		let $ele = $(ele.target);
		let self = this;
		let reqData = {
			paged: true,
        	pageNo: 1,
        	pageSize: 10
		};
		reqData[$ele.data('key')] = $ele.data('id');

        $.ajax({
			url: $ele.data('url'),
			method: 'post',
			data: JSON.stringify(reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$ele.parent().parent().remove();
				}
				else {
					alert('删除失败');
				}
			}
        });
	}
}

export default new Operating();
